//
//  InfoClienteViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 23/07/13.
//
//

#import "InfoClienteViewController.h"
#import "Syncronize.h"
#import "Utente.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "Define.h"
#import "JsonCR2.h"
#import "ComunicationsUtils.h"
#import "DettagliInterventoViewController.h"
#import "FirmaInterventoViewController.h"
#import "CostiInterventoViewController.h"
#import "ClientiInterventoViewController.h"
#import "Buffer.h"
#import "Syncronize.h"
#import "CoreDataManager.h"
#import "ManagedObjectCloner.h"
#import "Util.h"
#import "OpenInterventoViewController.h"
#import "NoteClienteViewController.h"

@interface InfoClienteViewController ()

@end

@implementation InfoClienteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    return self;
}
-(void ) viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [super viewDidLoad];
    
    
    self.array = [NSMutableArray arrayWithCapacity:0];
    [self.array addObject:@"Nominativo"];
    [self.array addObject:@"Codice Fiscale"];
    [self.array addObject:@"Partita IVA"];
    [self.array addObject:@"Telefono"];
    [self.array addObject:@"EMail"];
    [self.array addObject:@"Fax"];
    [self.array addObject:@"Referente"];
    [self.array addObject:@"Città"];
    [self.array addObject:@"Indirizzo"];
    [self.array addObject:@"Interno"];
    [self.array addObject:@"Ufficio"];
    [self.array addObject:@"Annotazioni"];
    
    
    self.button = @"null";
    self.incomplete = false;
    NSLog(@"%@",[self.coreDataManager calcStatusCliente:self.cliente]);

    
    if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"MOD"] ||
       [[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"NEW"]
       ){
        self.canc.hidden = false;
    }else{
        self.canc.hidden = true;
    }
    
}

-(BOOL) isModify{
    if(self.hashCLient != [CoreDataManager hashcodeCliente:self.cliente]  ||
       [[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"MOD"] )return true;
    else  return false;
}



- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.table reloadData];
}

-(IBAction) cancella{
    self.button = @"del";
    if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"MOD"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Ripristino"
                                                              message:@"Ripristina il cliente?"
                                                             delegate:self
                                                    cancelButtonTitle:@"Ripristina"
                                                    otherButtonTitles:@"Completa", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
        [myAlertView show];
    }
    else{
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Eliminare?"
                                                              message:@"Eliminare il cliente?"
                                                             delegate:self
                                                    cancelButtonTitle:@"Elimina"
                                                    otherButtonTitles:@"Annulla", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
        [myAlertView show];
    }
}

-(IBAction) indietro{
    self.button = @"back";
    
    if([self isModify] == false){
        if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"NEW"]){
            NSLog(@"notModify : NEW");
            [self salvaESeleziona];
        }
        else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"MOD"]){
            NSLog(@"notModify : MOD");
            [self salvaESeleziona];
        }
        else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"SAVE"]){
            NSLog(@"notModify : SAVE");
            [self ritornaInList];
        }else{
            NSLog(@"notModify : NULL");
            [self ritornaInList];
        }
    }else{
        if([self.coreDataManager checkCliente:self.cliente]){
            if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"NEW"]){
                NSLog(@"isModify: complete : NEW");
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Salva"
                                                                      message:@"Aggiungere il nuovo cliente?"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Annulla"
                                                            otherButtonTitles:@"Salva", nil];
                [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
                [myAlertView show];
            }
            else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"SAVE"]){
                NSLog(@"isModify: complete : SAVE");
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Salva"
                                                                      message:@"Il Cliente risulta modificato. Salvare le modifiche?"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Annulla"
                                                            otherButtonTitles:@"Salva", nil];
                [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
                [myAlertView show];
            }
            else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"MOD"]){
                NSLog(@"isModify: complete : MOD");
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Salva"
                                                                      message:@"Il Cliente risulta modificato. Salvare le modifiche?"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Annulla"
                                                            otherButtonTitles:@"Salva", nil];
                [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
                [myAlertView show];
            }else{
                NSLog(@"isModify: complete : NULL");
            }
        }else{
            
            self.incomplete = true;
            
            if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"NEW"]){
                NSLog(@"isModify: incomplete : NEW");
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Dati Incompleti"
                                                                      message:@"Eliminare o Completare il cliente appena creato?"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Elimina"
                                                            otherButtonTitles:@"Completa", nil];
                [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
                [myAlertView show];
            }
            else if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"SAVE"]){
                NSLog(@"isModify: incomplete : SAVE");
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Dati Incompleti"
                                                                      message:@"Ripristina il cliente?"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Ripristina"
                                                            otherButtonTitles:@"Completa", nil];
                [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
                [myAlertView show];
            }
            else if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"MOD"]){
                NSLog(@"isModify: incomplete : MOD");
                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Dati Incompleti"
                                                                      message:@"Ripristina il cliente?"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Ripristina"
                                                            otherButtonTitles:@"Completa", nil];
                [myAlertView setAlertViewStyle:UIAlertViewStyleDefault];
                [myAlertView show];
            }else{
                NSLog(@"isModify: incomplete : NULL");
            }
        }
    }
}

- (void)ritornaInList{
    [[self navigationController] popViewControllerAnimated:YES];
}

- (void)ritornaInIntervento{
    OpenInterventoViewController *c = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3];
    c.intervento.cliente = self.cliente;
    [[self navigationController] popToViewController:c animated:YES];
}
- (void) eliminaRitornaInList{
    self.intervento.cliente = nil;
    [self.coreDataManager delClienteObject:self.cliente];
    [[self navigationController] popViewControllerAnimated:YES];
}
- (void) refreshRitornaInList{
    [self.coreDataManager refresh:self.cliente];
    [self ritornaInList];
}
-(void) salvaESeleziona{
    self.cliente.buffer = @1;
    self.intervento.cliente = self.cliente;
    [self ritornaInIntervento];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if([self.button isEqualToString:@"null"]){
        [self ritornaInIntervento];
    }
    else if([self.button isEqualToString:@"del"]){
        if(buttonIndex == 0){
            if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"NEW"]){
                [self eliminaRitornaInList];
            }
            else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"MOD"]){
                [self refreshRitornaInList];
            }
            else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"SAVE"]){
                [self refreshRitornaInList];
            }
        }
    }
    else if([self.button isEqualToString:@"tap"]){
        [self saveValues:alertView];
        [self.table reloadData];
    }
    else if([self.button isEqualToString:@"back"]){
            if([self.coreDataManager checkCliente:self.cliente]){
                if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"NEW"]){
                    if(buttonIndex == 0){
                        NSLog(@"annulla : complete : NEW");
                    }
                    if(buttonIndex == 1){
                        NSLog(@"salva : complete : NEW");
                        [self salvaESeleziona];
                    }
                }
                else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"SAVE"]){
                    if(buttonIndex == 0){
                        NSLog(@"annulla : complete : SAVE");
                    }
                    if(buttonIndex == 1){
                        NSLog(@"salva : complete : SAVE");
                        [self salvaESeleziona];
                    }
                }
                else if([[self.coreDataManager calcStatusCliente:self.cliente]  isEqualToString:@"MOD"]){
                    if(buttonIndex == 0){
                        NSLog(@"annulla : complete : MOD");
                    }
                    if(buttonIndex == 1){
                        NSLog(@"salva : complete : MOD");
                        [self salvaESeleziona];
                    }
                }else{
                    NSLog(@"btn: complete : NULL");
                }
            }else{
                if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"NEW"]){
                    if(buttonIndex == 0){
                        NSLog(@"elimina : incomplete : NEW");
                        [self eliminaRitornaInList];
                    }
                    if(buttonIndex == 1){
                        NSLog(@"completa : incomplete : NEW");
                    }
                }
                else if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"SAVE"]){
                    if(buttonIndex == 0){
                        NSLog(@"elimina : incomplete : SAVE");
                        [self eliminaRitornaInList];
                    }
                    if(buttonIndex == 1){
                        NSLog(@"completa : incomplete : SAVE");
                    }
                }
                else if([[self.coreDataManager calcStatusCliente:self.cliente] isEqualToString:@"MOD"]){
                    if(buttonIndex == 0){
                        NSLog(@"elimina : incomplete : MOD");
                        [self eliminaRitornaInList];
                    }
                    if(buttonIndex == 1){
                        NSLog(@"completa : incomplete : MOD");
                    }
                }else{
                    NSLog(@"isModify: incomplete : NULL");
                }
            }
    }
}

-(void) saveValues:(UIAlertView *)alertView {
    NSString *name = self.nameSelect;
    if([name isEqualToString:@"Nominativo"]){
        self.cliente.nominativo = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Codice Fiscale"]){
        self.cliente.codice_fiscale = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Partita IVA"]){
        self.cliente.partita_iva = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Telefono"]){
        self.cliente.telefono = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"EMail"]){
        self.cliente.email = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Fax"]){
        self.cliente.fax = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Referente"]){
        self.cliente.referente = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Città"]){
        self.cliente.citta = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Indirizzo"]){
        self.cliente.indirizzo = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Interno"]){
        self.cliente.interno = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Ufficio"]){
        self.cliente.ufficio = [[alertView textFieldAtIndex:0]text];
    }
    else if([name isEqualToString:@"Annotazioni"]){
        for(int i=0; i<alertView.subviews.count;i++){
            if ([[alertView.subviews objectAtIndex:i] isKindOfClass:[UITextView class]]) {
                UITextView *tv = [alertView.subviews objectAtIndex:i];
                self.cliente.note = tv.text;
            }
        }
    }
    else{
    }
    [self.table reloadData];
}


// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self.intervento.idintervento intValue]<0)return @"Cliente Nuovo Intervento";
    return [NSString stringWithFormat: @"Cliente Intervento %@", self.intervento.numero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    
    NSString *name = [self.array objectAtIndex:indexPath.row];
    if([name isEqualToString:@"Nominativo"] && self.cliente.nominativo != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.nominativo;
        if([self.cliente.nominativo isEqualToString:@""] && [self.cliente.nominativo isEqual:[NSNull null]]){
            [Util setCellAlert:cell];
        }
    }
    else if([name isEqualToString:@"Codice Fiscale"] && self.cliente.codice_fiscale != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.codice_fiscale;
    }
    else if([name isEqualToString:@"Partita IVA"] && self.cliente.partita_iva != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.partita_iva;
    }
    else if([name isEqualToString:@"Telefono"]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.telefono;
        if([self.cliente.telefono isEqualToString:@""] || [self.cliente.telefono isEqual:[NSNull null]] || self.cliente.telefono == nil){
            [Util setCellAlert:cell];
        }
    }
    else if([name isEqualToString:@"EMail"] && self.cliente.email != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.email;
    }
    else if([name isEqualToString:@"Fax"] && self.cliente.fax != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.fax;
        
    }
    else if([name isEqualToString:@"Referente"] && self.cliente.referente != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.referente;
    }
    else if([name isEqualToString:@"Città"] && self.cliente.citta != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.citta;
        if([self.cliente.citta isEqualToString:@""] || [self.cliente.citta isEqual:[NSNull null]]){
            [Util setCellAlert:cell];
        }
    }
    else if([name isEqualToString:@"Indirizzo"] && self.cliente.indirizzo != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.indirizzo;
        if([self.cliente.indirizzo isEqualToString:@""] || [self.cliente.indirizzo isEqual:[NSNull null]]){
            [Util setCellAlert:cell];
        }
    }
    else if([name isEqualToString:@"Interno"] && self.cliente.interno != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.interno;
    }
    else if([name isEqualToString:@"Ufficio"] && self.cliente.ufficio != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.ufficio;
    }
    else if([name isEqualToString:@"Annotazioni"] && self.cliente.note != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.cliente.note;
        
        
    }
    
    
    else{
        cell.textLabel.text = name;
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [self.array objectAtIndex:indexPath.row];
    self.nameSelect = name;
    
    self.button = @"tap";
    
    if([name isEqualToString:@"Nominativo"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Nominativo"
                                                              message:@"Nominativo dell'utente" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.nominativo isEqual:[NSNull null]] && self.cliente.nominativo != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.nominativo];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Codice Fiscale"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Codice Fiscale"
                                                              message:@"Inserisci il codice fiscale"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.codice_fiscale isEqual:[NSNull null]]&& self.cliente.codice_fiscale != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.codice_fiscale];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Partita IVA"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Partita Iva"
                                                              message:@"Inserisci la Partita IVA"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.partita_iva isEqual:[NSNull null]]&& self.cliente.partita_iva != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.partita_iva];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Telefono"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Telefono"
                                                              message:@"Inserisci il Telefono"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.telefono isEqual:[NSNull null]] && self.cliente.telefono != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.telefono];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"EMail"] ){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"EMail"
                                                              message:@"Inserisci l'indirizzo Mail"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.email isEqual:[NSNull null]] && self.cliente.email != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.email];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Fax"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Fax"
                                                              message:@"Inserisci il Fax"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.fax isEqual:[NSNull null]] && self.cliente.fax != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.fax];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Referente"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Fax"
                                                              message:@"Inserisci il Referente"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.referente isEqual:[NSNull null]] && self.cliente.referente != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.referente];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Città"] ){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Città"
                                                              message:@"Inserisci la Città"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.citta isEqual:[NSNull null]] && self.cliente.citta != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.citta];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Indirizzo"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Indirizzo"
                                                              message:@"Inserisci l'indirizzo"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.indirizzo isEqual:[NSNull null]] && self.cliente.indirizzo != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.indirizzo];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Interno"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Interno"
                                                              message:@"Inserisci l'interno"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.interno isEqual:[NSNull null]] && self.cliente.interno != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.interno];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Ufficio"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Ufficio"
                                                              message:@"Inserisci l'ufficio"
                                                             delegate:self
                                                    cancelButtonTitle:@"Annulla"
                                                    otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.cliente.ufficio isEqual:[NSNull null]] && self.cliente.ufficio != nil){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.cliente.ufficio];
        }
        [myAlertView show];
    }
    else if([name isEqualToString:@"Annotazioni"]){
        NoteClienteViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"NoteClienteViewController"];
        open.cliente = self.cliente;
        open.user = self.user;
        [self.navigationController pushViewController:open animated:TRUE];
    }
    
    else{
    }
}




@end
